﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using SharppiFlowEditor.controllers;
using SharppiFlowEditor.views;


namespace SharppiFlowEditor.models
{
    class ConditionalNode : AbstractNode
    {

        public ConditionalNode(int x, int y, int width, int height)
        {
            this._boundingRect = new Rectangle(x, y, width, height);
            
        }

        public override void draw(Graphics g)
        {
            draw(g, 1.0);
        }

        public override void draw(Graphics g, double zoomSize)
        {
            Title = formatTitleString(Title);            

            SizeF titleSize = g.MeasureString(Title, nodeFont);
            Pen border = new Pen(Color.Black, 0.5F);

            _boundingRect.Width = Math.Max(50, (int)titleSize.Width);
            _boundingRect.Height = Math.Max(50, (int)titleSize.Height);

            Rectangle relativeSizeRect = getRelativeBoundingRect(zoomSize);

            Point left, top, right, bottom;
            left = new Point(relativeSizeRect.Location.X - 30, relativeSizeRect.Location.Y + (relativeSizeRect.Height / 2));
            top = new Point(relativeSizeRect.Location.X + (relativeSizeRect.Width / 2), (relativeSizeRect.Location.Y - 20));
            right = new Point(relativeSizeRect.Location.X + relativeSizeRect.Width + 30, relativeSizeRect.Y + (relativeSizeRect.Height / 2));
            bottom = new Point(relativeSizeRect.Location.X + (relativeSizeRect.Width / 2), relativeSizeRect.Y + relativeSizeRect.Height + 20);

            Point [] points = new Point[] {left, top, right, bottom};
            
            if (BGColor != null)
            {
                g.FillPolygon(new SolidBrush(BGColor), points);                
            }
            g.DrawPolygon(border, points);
            g.DrawString(Title, nodeFont, 
                        new SolidBrush(Color.Black),
                        relativeSizeRect.X + (relativeSizeRect.Width / 2 - (int)titleSize.Width / 2),
                        relativeSizeRect.Y + (relativeSizeRect.Height / 2 - (int)titleSize.Height / 2));
        }

        public override Rectangle getBoundingRect()
        {
            return this._boundingRect;
        }

        public override void setBoundingRectLocation(int x, int y)
        {
            this._boundingRect.Location = new Point(x, y);
        }

        private string formatTitleString(string title)
        {
            int length = title.Length;
            string formatted = title;
            if (!formatted.Contains("  "))
            {
                for (int i = 0; i < length; i++)
                {
                    if (i > 1 && i % 20 == 0)
                        formatted = formatted.Insert(i, "\n");
                }
                formatted = formatted + "  ";
            }

            
            
            return formatted;
        }
    }
}
